using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using PocketLibrary;

namespace PocketApps
{
    public partial class PostPreview : Form
    {
        public PocketLibrary.BlogPost PostToPreview = null;

        //TBSettings.Settings Settings = TBSettings.Settings.Create();
        public PostPreview()
        {
            InitializeComponent();
        }

        private void PostPreview_Load(object sender, EventArgs e)
        {
            if (PostToPreview == null)
                webBrowser1.DocumentText = "No post";
            else
            {
                webBrowser1.DocumentText = BuildHtmlPost(PostToPreview);
            }
        }

        private string BuildHtmlPost(PocketLibrary.BlogPost PostToPreview)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
            sb.Append(string.Format("<html><head><title>{0}</title>", PostToPreview.postTitle));
            string datapath = HelperUtils.ApplicationPath + "\\settings\\";
            //datapath = datapath.Replace("\\", "/");
            //sb.Append("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + datapath + "default.css\" />");

            sb.Append("<style>");
            /*
            sb.Append("body { background-color: #ccccff; }");
            sb.Append("#content");
            sb.Append("{");
            sb.Append("font-family: Tahoma, Monospace;");
            sb.Append("background-color: white;");
            sb.Append("border: gray thin solid;");
            sb.Append("padding: 2px 2px 2px 2px;");
            sb.Append("margin: 5px;");
            sb.Append("}");
             * */
            using (StreamReader sr = new StreamReader(datapath + "default.css"))
            {
                while (sr.Peek() != -1)
                    sb.Append(sr.ReadLine());
            }

            sb.Append("</style>");

            sb.Append("</head><body>");
            // TODO: content goes here.
            sb.Append(string.Format("<div id=\"content\"><h1>{0}</h1>", PostToPreview.postTitle));

            sb.Append(ParseBody(PostToPreview.postBody));

            sb.Append("</body></html>");

            return sb.ToString();
        }

        private string ParseBody(string input)
        {
            RegexOptions options = RegexOptions.IgnoreCase & RegexOptions.ExplicitCapture;
            // FIXME
            // put tags (pbiuo) to an array
            // loop array with regex
            Regex regex = new Regex(@"\[([pbiuo])\](.+)\[\/[pbiuo]\]", options);

            MatchEvaluator myEval = new MatchEvaluator(ParagraphEval);

            input = regex.Replace(input, myEval);

            regex = new Regex(@"(\[url\])(.+)(\[\/url\])");

            myEval = new MatchEvaluator(HrefEval);

            input = regex.Replace(input, myEval);

            regex = new Regex(@"(\[url=(.+)\])(.+)(\[\/url\])");

            myEval = new MatchEvaluator(HrefEval2);

            input = regex.Replace(input, myEval);


            return input;
        }

        private string ParagraphEval(Match m)
        {
            return string.Format("<{0}>{1}</{0}>", m.Groups[1].ToString(), m.Groups[2].ToString());
        }

        private string HrefEval(Match m)
        {
            return string.Format("<a href=\"{0}\">{0}</a>", m.Groups[2].ToString());
        }

        private string HrefEval2(Match m)
        {
            return string.Format("<a href=\"{0}\">{1}</a>", m.Groups[2].ToString(), m.Groups[3].ToString());
        }
    }
}